//
//  HomeTrackYuYingView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/26.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "HomeTrackYuYingView.h"

@interface HomeTrackYuYingView ()
{
    UIBezierPath * path1;
    UIBezierPath * path2;
    UIBezierPath * path3;
    UIBezierPath * path4;
    UIBezierPath * path5;
    //
    CAShapeLayer * layer1;
    CAShapeLayer * layer2;
    CAShapeLayer * layer3;
    CAShapeLayer * layer4;
    CAShapeLayer * layer5;
    //
    CGFloat offset;
    CGFloat lastAccelera;
    //
    CGFloat bottomViewHeight;
}
@end

@implementation HomeTrackYuYingView

-(id)initWithBottomView{
    if (self = [super initWithFrame:kAppWindow.bounds inSuperView:kAppWindow]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];//145
        bottomViewHeight = kHeightProportion_In_iPhoneTriploid(400);
        GLGWView * pickViewBankgroundView = [[GLGWView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, bottomViewHeight) inSuperView:self];
        pickViewBankgroundView.backgroundColor = [UIColor whiteColor];
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:3 options:UIViewAnimationOptionCurveEaseIn animations:^{
            pickViewBankgroundView.frame = CGRectMake(0, KScreenHeight - bottomViewHeight, KScreenWidth, bottomViewHeight);
        } completion:^(BOOL finished) {
            
        }];
        path1 = [UIBezierPath bezierPath];
        path2 = [UIBezierPath bezierPath];
        path3 = [UIBezierPath bezierPath];
        path4 = [UIBezierPath bezierPath];
        path5 = [UIBezierPath bezierPath];
        
        layer1 = [CAShapeLayer layer];
        layer1.strokeColor = [UIColor orangeColor].CGColor;
        layer1.fillColor = [UIColor clearColor].CGColor;
        layer1.lineWidth = 0.5;
        [pickViewBankgroundView.layer addSublayer:layer1];

        layer2 = [CAShapeLayer layer];
        layer2.strokeColor = [UIColor blueColor].CGColor;
        layer2.fillColor = [UIColor clearColor].CGColor;
        layer2.lineWidth = 0.5;
        [pickViewBankgroundView.layer addSublayer:layer2];

        layer3 = [CAShapeLayer layer];
        layer3.strokeColor = [UIColor greenColor].CGColor;
        layer3.fillColor = [UIColor clearColor].CGColor;
        layer3.lineWidth = 0.5;
        [pickViewBankgroundView.layer addSublayer:layer3];

        layer4 = [CAShapeLayer layer];
        layer4.strokeColor = [UIColor purpleColor].CGColor;
        layer4.fillColor = [UIColor clearColor].CGColor;
        layer4.lineWidth = 0.5;
        [pickViewBankgroundView.layer addSublayer:layer4];
        
        layer5 = [CAShapeLayer layer];
        layer5.strokeColor = [UIColor cyanColor].CGColor;
        layer5.fillColor = [UIColor clearColor].CGColor;
        layer5.lineWidth = 0.5;
        [pickViewBankgroundView.layer addSublayer:layer5];
        //
        lastAccelera = 6;
        //根据手机刷新频率刷新曲线
        CADisplayLink * wavesDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawWaveLine)];
        [wavesDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    return self;
}

-(void)drawWaveLine{
    [path1 removeAllPoints];
    [path2 removeAllPoints];
    [path3 removeAllPoints];
    [path4 removeAllPoints];
    [path5 removeAllPoints];
    
    [path1 moveToPoint:CGPointMake(0, bottomViewHeight / 2)];
//    DLog(@"----------------------%f",self.bounds.size.height);
//    DLog(@"----------------------%f",self.bounds.size.width);
    [path2 moveToPoint:CGPointMake(0, bottomViewHeight / 2)];
    [path2 addLineToPoint:CGPointMake(5, bottomViewHeight / 2)];

    [path3 moveToPoint:CGPointMake(0, bottomViewHeight / 2)];
    [path3 addLineToPoint:CGPointMake(10, bottomViewHeight / 2)];

    [path4 moveToPoint:CGPointMake(0, bottomViewHeight/ 2)];
    [path4 addLineToPoint:CGPointMake(15, bottomViewHeight / 2)];
    
    [path5 moveToPoint:CGPointMake(0, bottomViewHeight / 2)];
    [path5 addLineToPoint:CGPointMake(20, bottomViewHeight / 2)];

    for (int x=0; x< self.bounds.size.width; x++) {
//        在想一下通式：y=Asin(wx+u)+K 对就是这么简单，那参数代表的又是什么呢?
//        A ：振幅
//        w ：角速度 w = 2PI/T T:周期
//        u ：初相
//        K ：y 轴偏移
        double y = 9 * lastAccelera *sin(M_PI/(double)(KScreenWidth/6) * (double)x + (double)offset)*(sin((double)x * M_PI /(double)KScreenWidth));

        [path1 addLineToPoint:CGPointMake((double)x, y+(double)(bottomViewHeight/2))];
        //
        if ((float)x < self.bounds.size.width -5) {
            double y1 = 8 * lastAccelera * sin(M_PI/(double)(KScreenWidth/6) * (double)x + (double)offset -0.4) * (sin((double)x * M_PI/(double)(KScreenWidth - 5)));
            [path2 addLineToPoint:CGPointMake(x +5, y1 + bottomViewHeight/2)];
        }

        if ((CGFloat)x < self.bounds.size.width - 10) {
            double y2 = 7 * lastAccelera * sin(M_PI/(double)(KScreenWidth/6) * (double)x + (double)offset-0.6) * (sin((double)x * M_PI / (double)(KScreenWidth - 10)));
            [path3 addLineToPoint:CGPointMake(x+10, y2 + (double)(bottomViewHeight/2))];
        }

        if ((CGFloat)x < self.bounds.size.width - 15) {
            double y3 = 6 * lastAccelera * sin(M_PI/(double)(KScreenWidth/6) *(double)x +(double)offset-0.8) * (sin((double)x * M_PI/(double)(KScreenWidth - 15)));
            [path4 addLineToPoint:CGPointMake(x+15, y3+ (double)(bottomViewHeight/2))];
        }
        
        if ((CGFloat)x < self.bounds.size.width - 20) {
            double y3 = 6 * lastAccelera * sin(M_PI/(double)(KScreenWidth/6) *(double)x +(double)offset-10) * (sin((double)x * M_PI/(double)(KScreenWidth - 20)));
            [path5 addLineToPoint:CGPointMake(x+20, y3+ (double)(bottomViewHeight/2))];
        }
    }
    [path1 addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2)];
    [path2 addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2)];
    [path3 addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2)];
    [path4 addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2)];
    [path4 addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2)];
    
    layer1.path = path1.CGPath;
    layer2.path = path2.CGPath;
    layer3.path = path3.CGPath;
    layer4.path = path4.CGPath;
    layer5.path = path5.CGPath;

    //没有加速就显示文案，波浪线归0
    if (lastAccelera <= 0.1) {
        lastAccelera = 0.0;
//        self.noMotionlabel.isHidden = false
    }
    if (lastAccelera <= 0.1) {
        lastAccelera = 0.0;
//        self.noMotionlabel.isHidden = false
    }else{
        lastAccelera -= 0.08;
//        self.noMotionlabel.isHidden = true
    }
    //波浪线移动
    offset -= 0.18;
    if (offset < -60 * M_PI) {
        offset = 0;
    }
}

@end
